package com.ronri.ams.sample;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ExpandableListView;
import android.widget.ListView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cz.nongyetongv5.R;
import com.cz.nongyetongv5.application.InitApplication;
import com.ronri.ams.bean.SamplingBean;
import com.ronri.common.StringManager;
import com.ronri.list.editview.AbstractItemElement;
import com.ronri.list.editview.DefaultItemElement;
import com.ronri.list.editview.ExtendListEditAdapter;
import com.ronri.list.editview.ListEditAdapter;
import com.ronri.list.editview.controls.EditCardTextBox;

import java.util.ArrayList;
import java.util.List;

import static com.cz.nongyetongv5.application.InitApplication.gSampleInfo;
import static com.ronri.ams.SystemEnvironmental.Sample_INPUT_AREA;
import static com.ronri.ams.SystemEnvironmental.Sample_INPUT_HUMEN;
import static com.ronri.ams.SystemEnvironmental.Sample_INPUT_NATURE;

/**
 * 文件：ams.java
 * 包名：com.ronri.ams.sample;
 * 说明：
 * 作者：MS During
 * 日期：2017/5/31
 * 历史：
 */
public class activity_map_search extends abstract_activity {

    private List<AbstractItemElement> mItems = new ArrayList<AbstractItemElement>();

    private ExtendListEditAdapter mAdapter;

    private SamplingBean mSample = null;

    private String mSectionDict = "";

    private String mObject = "";
    private int mHangye =1;
    private int mEnt = 1;
    private String mRegionCode = "";
    private String mRegionName = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        String szJson = "";
        setContentView(R.layout.activity_sample_edit);
        Log.i("aaa","activity_map_search");
        // Bundle bundle = getIntent().getExtras();

        //mSectionDict = bundle.getString("dict");

        ExpandableListView listView = (ExpandableListView) findViewById(R.id.list_edit);

        setTitle("常规检索", "确定");
        init();

        mRegionCode = gSampleInfo.getSample_area();

        CreateDemo();
        // 隐藏系统箭头
        listView.setGroupIndicator(null);

        mAdapter = new ExtendListEditAdapter(this, mItems, new ListEditAdapter.Callback() {
            @Override
            public void click(View v) {

            }
        });


        listView.setAdapter(mAdapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(getBaseContext(), "Children Click", Toast.LENGTH_LONG);
            }
        });

        listView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
            @Override
            public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
                switch (groupPosition) {
                    case 0: { // 抽样区域
                        Intent intent = new Intent(getApplicationContext(), activity_region_select.class);
                        startActivityForResult(intent, Sample_INPUT_AREA);
                    }
                    break;
                    case 1: { // 对象类型
                        Intent intent = new Intent(getApplicationContext(), activity_control_select.class);
                        Bundle bundle = new Bundle();

                        /*** 设置远程地址 ***/
                        // bundle.putString("remoteAddress", getResources().getString(R.string.remote_get_sample_nature));
                        bundle.putString("data", "[{\"name\":\"企业\",\"num\":1},{\"name\":\"合作社\",\"num\":2},{\"name\":\"种养殖户\",\"num\":7},{\"name\":\"其他\",\"num\":4}]");
                        bundle.putString("filterKeyName", "name");
                        bundle.putString("viewTitleName", "name");
                        bundle.putInt("control_id", groupPosition);
                        bundle.putInt("selectMode", ListView.CHOICE_MODE_SINGLE);
                        bundle.putString("title", "对象类型");
                        bundle.putString("vtitle", "确定");

                        intent.putExtras(bundle);

                        startActivityForResult(intent, Sample_INPUT_NATURE);
                    }
                    break;
                    case 2: {   // 对象行业
                        Intent intent = new Intent(getApplicationContext(), activity_control_select.class);
                        Bundle bundle = new Bundle();

                        /*** 设置远程地址 ***/
                        bundle.putString("data", "[{\"name\":\"种植业\",\"num\":1},{\"name\":\"畜牧业\",\"num\":2},{\"name\":\"渔业\",\"num\":3}]");
                        bundle.putString("filterKeyName", "name");
                        bundle.putString("viewTitleName", "name");
                        bundle.putInt("control_id", groupPosition);
                        bundle.putInt("selectMode", ListView.CHOICE_MODE_SINGLE);
                        bundle.putString("title", "对象行业");
                        bundle.putString("vtitle", "确定");
                        bundle.putString("value", gSampleInfo.getSection());

                        intent.putExtras(bundle);

                        startActivityForResult(intent, Sample_INPUT_HUMEN);
                    }
                    break;
                    case 3: {   // 受检对象
                        // 检索并弹出选择框
                        Intent intent = new Intent(InitApplication.getContext(), activity_search_map.class);
                        Bundle bundle = new Bundle();
                        bundle.putString("regioncode", mRegionCode);
                        bundle.putInt("kind", mEnt);
                        bundle.putInt("trade", mHangye);
                        intent.putExtras(bundle);
                        startActivityForResult(intent, 12000);
                    }
                    break;
                }

                return false;
            }
        });

        /****
         * 列表Item点击事件
         */
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(getBaseContext(), "setOnItemClickListener Click", Toast.LENGTH_LONG);
            }
        });
    }

    /****
     * 获取数据并反馈数据
     * @return
     */
    private void getRemoteData(){



    }

    /****
     * 创建DEMO
     */
    private void CreateDemo() {
        if (mSample == null)
            mSample = new SamplingBean();

        AbstractItemElement iTems = new DefaultItemElement(this,
                "监测区域",
                gSampleInfo.getArea_name(),
                null,
                getResources().getDrawable(R.drawable.jiandujiancha_icon_more),
                "fRegion",
                "", false, new EditCardTextBox());

        mItems.add(iTems);

        iTems = new DefaultItemElement(this,
                "对象类型",
                "企业",
                null,
                getResources().getDrawable(R.drawable.jiandujiancha_icon_more),
                "fRegion",
                "", false, new EditCardTextBox());

        mItems.add(iTems);

        String szHumen = gSampleInfo.getUser_name();

        if (StringManager.isEmpty(szHumen))
            szHumen = "";

        iTems = new DefaultItemElement(this,
                "对象行业",
                "种植业",
                null,
                getResources().getDrawable(R.drawable.jiandujiancha_icon_more),
                "fRegion",
                "", false, new EditCardTextBox());

        mItems.add(iTems);

        iTems = new DefaultItemElement(this,
                "受检对象",
                gSampleInfo.getInspectd_name(),
                null,
                getResources().getDrawable(R.drawable.jiandujiancha_icon_more),
                "fRegion",
                "", false, new EditCardTextBox());

        mItems.add(iTems);
    }

    /***
     * 接收弹出框输入的结果
     *
     * @param requestCode
     * @param resultCode
     * @param data
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case Sample_INPUT_AREA: {
                    Bundle bundle = data.getExtras();
                    String szResult = bundle.getString("region_code");
                    if (szResult != null) {
                        String ss = bundle.getString("region_name");
                        mRegionCode = szResult;
                        gSampleInfo.setArea_name(ss);
                        gSampleInfo.setSample_area(szResult);
                        mItems.get(0).setInfo(ss);
                        mAdapter.notifyDataSetChanged();
                    }
                }
                break;
                case Sample_INPUT_NATURE: {
                    Bundle bundle = data.getExtras();
                    String szResult = bundle.getString("returnValue");

                    if (szResult != null) {
                        JSONArray jsonArray = JSON.parseArray(szResult);
                        if (jsonArray != null) {
                            JSONObject obj = (JSONObject) jsonArray.get(0);
                            if (obj.containsKey("name")) {
                                String ss = obj.getString("name");
                                mEnt = Integer.parseInt(obj.getString("num"));
                                mItems.get(1).setInfo(ss);
                                mAdapter.notifyDataSetChanged();
                                mSample.setdept(ss);
                            }
                        }
                    }
                }
                break;
                case Sample_INPUT_HUMEN: {
                    Bundle bundle = data.getExtras();
                    String szResult = bundle.getString("returnValue");

                    if (szResult != null) {
                        JSONArray jsonArray = JSON.parseArray(szResult);
                        if (jsonArray != null) {
                            JSONObject obj = (JSONObject) jsonArray.get(0);
                            if (obj.containsKey("name")) {
                                String ss = obj.getString("name");
                                mHangye = Integer.parseInt(obj.getString("num"));
                                mItems.get(2).setInfo(ss);
                                mAdapter.notifyDataSetChanged();
                                mSample.setdept(ss);
                            }
                        }
                    }
                }
                break;
                case 12000:{
                    gSampleInfo.setSample_area(mRegionCode);
                    gSampleInfo.setArea_name(mRegionName);
                    setResult(RESULT_OK, data);
                    finish();
                }
            }
        }

    }
}
